<?php


namespace app\service;


use think\Db;

class EalspellCommissionService
{
    static function RecommendCommission(){
        return array(
            "merchants"=>[0,0.2,0.25,0.30,0.35],
            "merchants_upgrade"=>[0,0.1,0.15,0.2,0.25],
            "merchants_consump"=>[0,0.05,0.1,0.15,0.2],
            "first_consump" => 2,
            "second_consump"=>2,
            "third_consump"=>5,
            "twelf_consump"=>38,
        );
    }

    static function addMemberConsump($commander_id,$price,$member_id,$order_number){
        $_return_array = array();
        $date = date("Y-m-d H:i:s");
        $deduct = 0.01;
        array_push($_return_array,array("ragent_id"=>$commander_id,"price"=>getsPriceFormat($price*$deduct),"type"=>8,
            "create_time"=>$date,"status"=>2,"source"=>9,"member_id"=>$member_id,"relation_type"=>1,"order_number"=>$order_number,"cid"=>0));
        Db::connect("ealspell")->table("community_agent")
            ->where(array("id"=>$commander_id))->setInc("balance",$price*$deduct);
        $middle_id = $commander_id;
        # 运营中心得千分之一
        $yunying_deduct = 0.001;
        for($i=0;$i<=4;$i++){
            $middle_info = Db::connect("ealspell")->table("community_agent")
                ->field("mark,pid")->where(array("id"=>$middle_id,"status"=>2))->find();
            if(!empty($middle_info)){
                if(!empty($middle_info['pid'])){
                    array_push($_return_array,array("ragent_id"=>$middle_info['pid'],"price"=>getsPriceFormat($price*$yunying_deduct),"type"=>10,
                        "create_time"=>$date,"status"=>2,"source"=>9,"member_id"=>$member_id,"relation_type"=>1,"order_number"=>$order_number,"cid"=>$middle_id));
                    Db::connect("ealspell")->table("community_agent")
                        ->where(array("id"=>$middle_id))->setInc("balance",getsPriceFormat($price*$yunying_deduct));
                    $middle_id = $middle_info['pid'];
                    continue;
                }else{
                    break;
                }

            }else{
                break;
            }
        }
        if(!empty($_return_array)){
            Db::connect("ealspell")->table('deduct_record')->insertAll($_return_array);
        }

    }
    /**
     * @param $commander_id
     * @return bool
     * @throws \think\Exception
     * @context 添加用户注册佣金
     */
    static function addRegisterCommission($commander_id,$member_id){
        $_return_array = array();
        $date = date("Y-m-d H:i:s");
        $first_info = Db::connect("ealspell")->table("community_agent")
            ->field("mark,pid")->where(array("id"=>$commander_id,"status"=>2))->find();
        if(!empty($first_info)){
            $first_price = 2;
            array_push($_return_array,array("ragent_id"=>$commander_id,"price"=>$first_price,"type"=>1,"create_time"=>$date,"status"=>2,
                "source"=>5,"cid"=>0,"member_id"=>$member_id,"relation_type"=>1));
            Db::connect("ealspell")->table("community_agent")
                ->where(array("id"=>$commander_id))->setInc("balance",$first_price);
            if(!empty($first_info['pid'])){
                $second_info = Db::connect("ealspell")->table("community_agent")
                    ->field("mark,pid")->where(array("id"=>$first_info['pid'],"status"=>2))->find();
                $second_price = 0.2;
                array_push($_return_array,array("ragent_id"=>$first_info['pid'],"price"=>$second_price,"type"=>11,"create_time"=>$date,"status"=>2,
                    "source"=>5,"cid"=>$commander_id,"member_id"=>$member_id,"relation_type"=>1));
                Db::connect("ealspell")->table("community_agent")
                    ->where(array("id"=>$first_info['pid']))->setInc("balance",$second_price);
                if($second_info['mark']==4){
                    array_push($_return_array,array("ragent_id"=>$first_info['pid'],"price"=>$second_price,"type"=>11,"create_time"=>$date,"status"=>2,
                        "source"=>5,"cid"=>$first_info['pid'],"member_id"=>$member_id,"relation_type"=>1));
                    Db::connect("ealspell")->table("community_agent")
                        ->where(array("id"=>$first_info['pid']))->setInc("balance",$second_price);
                }else{
                    # 查询上级是否有运营中心
                    $middle_id = $second_info['pid'];
                    $cid = $first_info['pid'];
                    for($i=0;$i<4;$i++){
                        $middle_info = Db::connect("ealspell")->table("community_agent")
                            ->field("mark,pid")->where(array("id"=>$middle_id,"status"=>2))->find();
                        if(!empty($middle_info)){
                            if($middle_info['mark']==4){
                                array_push($_return_array,array("ragent_id"=>$middle_id,"price"=>$second_price,"type"=>11,"create_time"=>$date,"status"=>2,
                                    "source"=>5,"cid"=>$cid,"member_id"=>$member_id,"relation_type"=>1));
                                Db::connect("ealspell")->table("community_agent")
                                    ->where(array("id"=>$middle_id))->setInc("balance",$second_price);
                                break;
                            }else{
                                $cid = $middle_id;
                                if(!empty($middle_info['pid'])){
                                    $middle_id = $middle_info['pid'];
                                    continue;
                                }else{
                                    break;
                                }

                            }
                        }else{
                            break;
                        }
                    }
                }
            }
        }
        if(!empty($_return_array)){
            Db::connect("ealspell")->table('deduct_record')->insertAll($_return_array);
        }
        return true;

    }

    /**
     * @param $commander_id
     * @param $price
     * @param $merchants_id
     * @return bool
     * @context 合作店抽成奖励
     */
    static function BizConsump($commander_id,$price,$merchants_id,$order_number){
        # 这里的price 已经是抽成金额了不需要*商家抽成比例
        $arr = self::RecommendCommission()['merchants_consump'];
        $_return_array = array();
        $date = date("Y-m-d H:i:s");
        $middle_id = $commander_id;
        $middle_price = 0;
        $type = 20;
        for($i=0;$i<=4;$i++){
            $middle_info = Db::connect("ealspell")->table("community_agent")
                ->field("mark,pid,point_mark")->where(array("id"=>$middle_id,"status"=>2))->find();
            if(!empty($middle_info)){
                $add_price = getsPriceFormat($price*($arr[$middle_info['mark']]-$middle_price));
                $middle_price = $arr[$middle_info['mark']];
                if($add_price>0){
                    array_push($_return_array,array("ragent_id"=>$middle_id,"price"=>$add_price,"type"=>$type,"create_time"=>$date,
                        "status"=>2,"source"=>10,"pro_id"=>$merchants_id,"order_number"=>$order_number,"biz_id"=>$merchants_id,
                        "relation_type"=>3));
                }
                $type = 12;
                Db::connect("ealspell")->table("community_agent")
                    ->where(array("id"=>$middle_id))->setInc("balance",getsPriceFormat($add_price));
                if(!empty($middle_info['pid'])){
                    $middle_id = $middle_info['pid'];
                    continue;
                }else{
                    break;
                }
            }else{
                break;
            }

        }
        if(!empty($_return_array)){
            Db::connect("ealspell")->table('deduct_record')->insertAll($_return_array);
        }
        return true;
    }
    /**
     * @param $commander_id
     * @param $merchants_id
     * @return bool
     * @throws \think\Exception
     * @context 合作店首次达成销售额给代理加提成
     */
    static function BizSalesAchieved($commander_id,$merchants_id){
        $price = 20;
        $sale_price = 300;
        $date = date("Y-m-d H:i:s");
        # 查询是否有该类型的记录
        //Db::table("merchants_logconsump")->
        $is_receive = Db::connect("ealspell")->table('deduct_record')
            ->where(array("ragent_id"=>$commander_id,"type"=>29,"pro_id"=>$merchants_id,"source"=>29))->find();
        if(empty($is_receive)){
            # 查询该商家收款是否达成金额
            $merchant_log=Db::table("orders")->where(array("biz_id"=>$merchants_id))->where("order_status=5 or order_status=7")->sum("pay_price");
            if($merchant_log>=$sale_price){
                Db::connect("ealspell")->table('deduct_record')->insert(array("ragent_id"=>$commander_id,"price"=>$price,
                    "type"=>29,"create_time"=>$date,"status"=>2,"source"=>29,"pro_id"=>$merchants_id,"biz_id"=>$merchants_id,
                    "relation_type"=>3));
                Db::connect("ealspell")->table("community_agent")
                    ->where(array("id"=>$commander_id))->setInc("balance",getsPriceFormat($price));
            }
        }
        return true;
    }

    /**
     * @param $commander_id
     * @param $price
     * @param $merchants_id
     * @return bool
     * @context 续费给提成
     */
    static function merchantsUpgrade($commander_id,$price,$merchants_id)
    {
        $arr = self::RecommendCommission()['merchants_upgrade'];
        $_return_array = array();
        $date = date("Y-m-d H:i:s");
        $middle_id = $commander_id;
        $middle_price = 0;
        $type = 5;
        for($i=0;$i<=4;$i++){
            $middle_info = Db::connect("ealspell")->table("community_agent")
                ->field("mark,pid,point_mark")->where(array("id"=>$middle_id,"status"=>2))->find();
            if(!empty($middle_info)){
                $add_price = getsPriceFormat($price*($arr[$middle_info['mark']]-$middle_price));
                $middle_price = $arr[$middle_info['mark']];
                array_push($_return_array,array("ragent_id"=>$middle_id,"price"=>$add_price,"type"=>$type,"create_time"=>$date,"status"=>2,
                    "source"=>11,"pro_id"=>$merchants_id,"biz_id"=>$merchants_id,"relation_type"=>3));
                Db::connect("ealspell")->table("community_agent")
                    ->where(array("id"=>$middle_id))->setInc("balance",getsPriceFormat($add_price));
                if(!empty($middle_info['pid'])){
                    $middle_id = $middle_info['pid'];
                    continue;
                }else{
                    break;
                }
            }else{
                break;
            }
        }
        if(!empty($_return_array)){
            Db::connect("ealspell")->table('deduct_record')->insertAll($_return_array);
        }
        return true;
    }

    /**
     * @param $commander_id
     * @param $price
     * @param $merchants_id
     * @return bool
     * @context 首次升级给提成
     */
    static function merchantsCharge($commander_id,$price,$merchants_id)
    {
        $arr = self::RecommendCommission()['merchants'];
        $_return_array = array();
        $date = date("Y-m-d H:i:s");
        $middle_id = $commander_id;
        $middle_price = 0;
        $type = 4;
        for($i=0;$i<=4;$i++){
            $middle_info = Db::connect("ealspell")->table("community_agent")
                ->field("mark,pid,point_mark")->where(array("id"=>$middle_id,"status"=>2))->find();
            if(!empty($middle_info)){
                $add_price = getsPriceFormat($price*($arr[$middle_info['mark']]-$middle_price));
                $middle_price = $arr[$middle_info['mark']];
                array_push($_return_array,array("ragent_id"=>$middle_id,"price"=>$add_price,"type"=>$type,"create_time"=>$date,"status"=>2,
                    "source"=>11,"pro_id"=>$merchants_id,"biz_id"=>$merchants_id,"relation_type"=>3));
                Db::connect("ealspell")->table("community_agent")
                    ->where(array("id"=>$middle_id))->setInc("balance",getsPriceFormat($add_price));
                if(!empty($middle_info['pid'])){
                    $middle_id = $middle_info['pid'];
                    continue;
                }else{
                    break;
                }
            }else{
                break;
            }
        }
        if(!empty($_return_array)){
            Db::connect("ealspell")->table('deduct_record')->insertAll($_return_array);
        }
        return true;
    }

    /**
     * @param $member_id
     * @param $agent_id
     * @param $order_number
     * @return bool
     * @context 用户第1，2，3 次消费给提成 不满100或使用卡券不给提
     */
    static function memberConsumpNum($member_id,$agent_id,$order_number){
        $date = date("Y-m-d H:i:s");
        # 判断汽车服务订单和商家订单总和是否是第一次
        # 汽车订单必须包含系统服务或商品
        $car_orders =  Db::table('orders')->where(array("member_id"=>$member_id))
            ->where("order_status in (1,5,6)")
            ->where("(select count(id) from order_biz where order_biz.order_number = orders.order_number and order_biz.custommark=1)>0
            or (select count(id) from order_server where order_server.order_number = orders.order_number and order_server.custommark=1)>0")
            ->count();
        $merchants_orders = Db::table("merchants_order")->where(array("member_id"=>$member_id))
            ->where("(actual_price>50 and (select count(id) from merchants_voucher_order where order_id = merchants_order.id)=0)
                        or (actual_price<=50)")
            ->where("order_status in (1,2) and pay_type in (1,2,3,4)")->count();
        $comm = self::RecommendCommission();
        if(($car_orders+$merchants_orders)<=12){
            if(($car_orders+$merchants_orders)==1){
                $price = $comm['first_consump'];
                $type = 21;
            }elseif (($car_orders+$merchants_orders)==2) {
                $price = $comm['second_consump'];
                $type = 22;
            }elseif(($car_orders+$merchants_orders)==3){
                $price = $comm['third_consump'];
                $type = 23;
            }elseif(($car_orders+$merchants_orders)==12){
                $price = $comm['twelf_consump'];
                $type = 24;
            }else{
                return true;
            }
            # 给代理加待结算佣金
            $_return_array = array(
                array("ragent_id"=>$agent_id,"price"=>$price,"type"=>2,
                    "create_time"=>$date,"status"=>1,"source"=>$type,"pro_id"=>0,"order_number"=>$order_number,"member_id"=>$member_id,
                    "relation_type"=>1,"cid"=>0)
            );
            $pinfo=Db::connect("ealspell")->table("community_agent")
                ->field("mark,pid")->where(array("id"=>$agent_id,"status"=>2))->find();
            # 给上级10%；
            if(!empty($pinfo['pid'])){
                array_push($_return_array,array("ragent_id"=>$pinfo['pid'],"price"=>$price*0.1,"type"=>10,
                    "create_time"=>$date,"status"=>1,"source"=>$type,"pro_id"=>0,"order_number"=>$order_number,"member_id"=>$member_id,
                    "relation_type"=>1,"cid"=>$agent_id)) ;
            }
            Db::connect("ealspell")->table('deduct_record')->insertAll($_return_array);
        }
        return true;
    }
    /**
     * @param $order_number
     * @param $member_id
     * @return bool
     * @throws \think\Exception
     * @context 消费完成后提成到账
     */
    static function memberConsumpNumFinish($order_number,$member_id){
        # 查询是否有代理提成
        $info = Db::connect("ealspell")->table("deduct_record")
            ->where(array("order_number"=>$order_number,"status"=>1,"member_id"=>$member_id))
            ->where("type=2 or type=10")
            ->where("source in (21,22,23,24)")
            ->select();
        if(!empty($info)){
            foreach($info as $v){
                Db::connect("ealspell")->table("community_agent")
                    ->where(array("id"=>$v['ragent_id']))->setInc("balance",getsPriceFormat($v['price']));
            }
            Db::connect("ealspell")->table("deduct_record")
                ->where(array("order_number"=>$order_number,"member_id"=>$member_id))
                ->where("type=2 and source in (21,22,23,24)")
                ->update(array("status"=>2));
        }
        return true;
    }
}